import { reactive } from 'vue'
import Cookies from 'js-cookie'

const TokenKey = 'TM-Token'

const store = {
    debug: false,

    state: reactive({
        appVersion: '-',
        token: '',
        authed: false,
    }),
    setAppVersion(newValue) {
        if (this.debug) {
            console.log('setAppVersion triggered with', newValue)
        }
        this.state.appVersion = newValue
    },
    // 从cookie获取token
    restoreTokenCookiedAction () {
        this.state.token = Cookies.get(TokenKey)
    },
    setTokenAction(newValue) {
        if (this.debug) {
        console.log('setTokenAction triggered with', newValue)
        }
        Cookies.set(TokenKey, newValue)
        this.state.token = newValue
    },
    clearTokenAction() {
        if (this.debug) {
        console.log('clearTokenAction triggered')
        }
        this.state.token = ''
        Cookies.remove(TokenKey)
    },
    setAuthedAction(newValue) {
        if (this.debug) {
            console.log('setAuthedAction triggered with', newValue)
        }
        this.state.authed = newValue
    },
}

export default store